package ext.tianma.part.finishedProduct.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class LoadAttrsConfig {
	// 配置文件路径
	public static String configFilePath = "/ptc/Windchill/codebase/ext/tianma/part/finishedProduct/config/finishedGoodAttrs.properties";
	/*public static String configFilePath = "codebase" + File.separator + "ext"
			+ File.separator + "tianma" + File.separator + "part"
			+ File.separator + "finishedProduct" + File.separator + "config"
			+ File.separator + "finishedGoodAttrs.properties";*/

	/**
	 * 根据配置文件路径读取分类节点对应的维护人员
	 * @param filePath 配置文件路径
	 * @return Map<分类全路经,List<角色名称>>
	 */
	public static Map<String,List<String>> getMaintainRolesName() {
		Map<String,List<String>> retMap = new HashMap<String,List<String>>();
		Properties prop = null;
		try {
			prop = getProperties(configFilePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (prop != null) {
			Enumeration en = prop.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String[] keyArray = key.split("\\.");
				String fullPath = keyArray[0];
				String roleName = keyArray[1];
				List<String> roleList = new ArrayList<String>();
				if(retMap.containsKey(fullPath )){
					roleList = retMap.get(fullPath);
				}else{
					roleList.add(roleName);
				}
				retMap.put(fullPath,roleList);
			}
		}
		return retMap;
	}
	
	/**
	 * 根据配置文件路径读取分类节点对应的维护人员
	 * @param classFullPath 分类全路径
	 * @return Map<分类全,List<角色名称>>
	 */
	public static List<String> getMaintainRolesName(String classFullPath,String finishedGoodDimension) {
		List<String> roleList = new ArrayList<String>();
		Properties prop = null;
		try {
			prop = getProperties(configFilePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (prop != null) {
			Enumeration en = prop.propertyNames();
			while (en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String[] keyArray = key.split("\\.");
				String fullPath = keyArray[0];
				String productDimension = keyArray[1];
				if((fullPath+ "." + productDimension).equals(classFullPath + "." + finishedGoodDimension)){
					String roleName = keyArray[2];
					if(roleList.contains((roleName))){
					}else{
						roleList.add(roleName);
					}
				}
			}
		}
		return roleList;
	}
	
	/**
	 * 根据配置文件路径获取Properties操作对象
	 * @param filePath   配置文件路径
	 * @return Properties
	 * @throws IOException 
	 */
	private static Properties getProperties(String filePath) throws IOException{
		Properties prop = null;
		FileInputStream inputStream = null;
		BufferedReader bf = null;
		try {
			prop = new Properties();
			inputStream = new FileInputStream(filePath);
            bf = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
			prop.load(bf);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null != inputStream){
				inputStream.close();
			}
			if(null != bf){
				bf.close();
			}
		}
		return prop;
	}

}
